package com.bwie.controller;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bwie.pojo.Goods;
import com.bwie.service.GoodsService;
import com.bwie.unit.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Type;

@RestController
@RequestMapping("user")
public class GoodsController {

      @Autowired
      private GoodsService goodsService;

    /**
     * 进行分页 查询 功能
     * @param goods
     * @return
     */
      @PostMapping("pageList")
      public R pageList(@RequestBody Goods goods){
          Page<Goods> goodsPage = new Page<>(goods.getPageNum(), goods.getPageSize());
          Page<Goods> page = goodsService.pageList(goodsPage,goods.getNumber());
          return R.OK(page);
      }

    /**
     * 通过功能
     * @param id
     * @return
     */
      @PostMapping("tong/{id}")
      public R tong(@PathVariable Integer id){
          Integer i = goodsService.updateStatus(id);
          if (i > 0){
              return R.OK();
          }else {
              return R.ERROR(500,"失败");
          }
      }

    /**
     * 不通过功能
     * @param id
     * @return
     */
    @PostMapping("puss/{id}")
    public R puss(@PathVariable Integer id){
        Integer i = goodsService.pussStatus(id);
        if (i > 0){
            return R.OK();
        }else {
            return R.ERROR(500,"失败");
        }
    }

    /**
     * 添加功能
     * @param goods
     * @return
     */
    @PostMapping("add")
    public R add(@RequestBody Goods goods){
        String s = RandomUtil.randomNumbers(4);
        goods.setNumber("WXD"+s);
        goods.setEncode("CF"+s);
        goodsService.save(goods);
        return R.OK();
    }

}
